package com.flame.shapeville.service;

import com.flame.shapeville.bean.LatestLogin;
import com.flame.shapeville.bean.User;
import com.flame.shapeville.factory.BeanFactory;
import com.flame.shapeville.util.DbConnector;

import java.util.Map;

/**
 * Service class responsible for managing user-related operations.
 * <p>
 * Includes functions for checking users, creating new users, retrieving current user,
 * and updating latest login information.
 *
 * @author Flame Dev Team
 * @version 1.0
 * @since 2025-05-01
 */
public class UserService {
    /**
     * Database connector for LatestLogin data.
     */
    private DbConnector<LatestLogin> latestLoginDAO;

    /**
     * Database connector for User data.
     */
    private DbConnector<User> userDAO;

    /**
     * Constructs a new UserService instance.
     * Initializes database connectors for User and LatestLogin.
     */
    public UserService() {
        latestLoginDAO = new DbConnector<>("db/LatestLogin.yml", LatestLogin.class);
        userDAO = new DbConnector<>("db/User.yml", User.class);
    }

    /**
     * Retrieves the currently logged-in user.
     *
     * @return The current User object or null if none found
     */
    public User getUser() {
        LatestLogin latestLogin = latestLoginDAO.querySingleton();
        return latestLogin == null ? null : userDAO.querySingleton("id", latestLogin.getUserId());
    }

    /**
     * Retrieves a user by their ID.
     *
     * @param id ID of the user
     * @return The User object or null if not found
     */
    public User getUser(String id) {
        return userDAO.querySingleton(id);
    }

    /**
     * Checks if a user with the given name exists in the database.
     *
     * @param name Name of the user to check
     * @return true if user exists, false otherwise
     */
    public boolean checkUser(String name) {
        User user = userDAO.querySingleton("name", name);
        if (user != null) {
            BeanFactory.setBean("user", user);
            return true;
        } else {
            return false;
        }
    }

    /**
     * Updates the latest login record with the current user's ID.
     */
    public void flushLatestLogin() {
        User user = (User) BeanFactory.getBean("user");
        LatestLogin latestLogin = latestLoginDAO.querySingleton();
        if (latestLogin == null) {
            latestLogin = new LatestLogin();
            latestLogin.setId("0");
        }
        latestLogin.setUserId(user.getId());
        latestLoginDAO.updateOrSave(latestLogin.getId(), latestLogin);
    }

    /**
     * Creates a new user with the given name and saves it to the database.
     *
     * @param name Name of the new user
     */
    public void createUser(String name) {
        String id = userDAO.getLastKey();
        if (id == null) {
            id = "0";
        } else {
            id = String.valueOf(Integer.parseInt(id) + 1);
        }
        User user = new User(id, name);
        userDAO.updateOrSave(user.getId(), user);
        BeanFactory.setBean("user", user);
    }
}
